package com.andy.blog.admin.service.impl;

import com.andy.blog.admin.service.CacheBusService;
import com.andy.blog.common.constant.CacheConstants;
import com.andy.blog.dal.domain.SysCache;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.connection.RedisServerCommands;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;

/**
 * Created by andy on 2023/5/13 14:45
 */
@Service
public class CacheBusServiceImpl implements CacheBusService {

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    private static final List<SysCache> caches = new ArrayList<>();

    static {
        caches.add(new SysCache(CacheConstants.LOGIN_TOKEN_KEY, "用户信息"));
        caches.add(new SysCache(CacheConstants.SYS_CONFIG_KEY, "配置信息"));
        caches.add(new SysCache(CacheConstants.SYS_DICT_KEY, "数据字典"));
        caches.add(new SysCache(CacheConstants.CAPTCHA_CODE_KEY, "验证码"));
        caches.add(new SysCache(CacheConstants.REPEAT_SUBMIT_KEY, "防重提交"));
        caches.add(new SysCache(CacheConstants.RATE_LIMIT_KEY, "限流处理"));
        caches.add(new SysCache(CacheConstants.PWD_ERR_CNT_KEY, "密码错误次数"));
    }

    @Override
    public Map<String, Object> getInfo() {
        Properties info = (Properties) redisTemplate.execute((RedisCallback<Object>) RedisServerCommands::info);
        Properties commandStats = (Properties) redisTemplate.execute((RedisCallback<Object>) connection -> connection.info("commandstats"));
        Object dbSize = redisTemplate.execute((RedisCallback<Object>) RedisServerCommands::dbSize);

        Map<String, Object> result = new HashMap<>(3);
        result.put("info", info);
        result.put("dbSize", dbSize);

        List<Map<String, String>> pieList = new ArrayList<>();
        if (Objects.nonNull(commandStats)) {
            commandStats.stringPropertyNames().forEach(key -> {
                Map<String, String> data = new HashMap<>(2);
                String property = commandStats.getProperty(key);
                data.put("name", StringUtils.removeStart(key, "cmdstat_"));
                data.put("value", StringUtils.substringBetween(property, "calls=", ",usec"));
                pieList.add(data);
            });
        }

        result.put("commandStats", pieList);
        return result;
    }

    @Override
    public List<SysCache> cache() {
        return caches;
    }

    @Override
    public Set<String> getCacheKeys(String cacheName) {
        return redisTemplate.keys(cacheName + "*");
    }

    @Override
    public SysCache getCacheValue(String cacheName, String cacheKey) {
        String cacheValue = redisTemplate.opsForValue().get(cacheKey);
        return new SysCache(cacheName, cacheKey, cacheValue);
    }

    @Override
    public void clearCacheName(String cacheName) {
        Collection<String> cacheKeys = redisTemplate.keys(cacheName + "*");
        if (CollectionUtils.isNotEmpty(cacheKeys)) {
            redisTemplate.delete(cacheKeys);
        }
    }

    @Override
    public void clearCacheKey(String cacheKey) {
        redisTemplate.delete(cacheKey);
    }

    @Override
    public void clearCacheAll() {
        Collection<String> cacheKeys = redisTemplate.keys("*");
        if (CollectionUtils.isNotEmpty(cacheKeys)) {
            redisTemplate.delete(cacheKeys);
        }
    }
}
